package uiLayer;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

import errorLayer.*;
import controlLayer.ClassCtrl;
import controlLayer.StaffCtrl;
import modelLayer.Class;
import modelLayer.Staff;

public class ClassGUI extends JPanel {
	
	private JTextField classIDField = new JTextField();
	private JTextField classNameField = new JTextField();
	private JTextField classStaffField = new JTextField();
	private JButton addBtn = new JButton("Add");
	private JButton editBtn = new JButton("Edit");
	private JButton removeBtn = new JButton("Remove");
	private JComboBox allClassesBox = new JComboBox();
	private List<Class> allClasses = new ArrayList<>();
	private ClassCtrl cc = new ClassCtrl();
	private StaffCtrl sc = new StaffCtrl();
	
	public ClassGUI () {
		
		setLayout(new BorderLayout(0, 0));
		
		allClasses = cc.allClasses();
		
		setup();
		
	}
	
	private void setup () {
		
		JPanel classContent = new JPanel();
		classContent.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLoweredBevelBorder(), "Class"));
		classContent.setLayout(new BorderLayout(0, 0));
		add(classContent, BorderLayout.NORTH);
		
		JPanel textNameArea = new JPanel();
		textNameArea.setLayout(new GridLayout(4, 1, 0, 5));
		classContent.add(textNameArea, BorderLayout.WEST);
		
		JPanel textArea = new JPanel();
		textArea.setBorder(new EmptyBorder(5, 5, 5, 5));
		textArea.setLayout(new GridLayout(4, 1, 0, 5));
		classContent.add(textArea, BorderLayout.CENTER);
		
		JPanel buttonArea = new JPanel();
		buttonArea.setBorder(new EmptyBorder(5, 5, 5, 5));
		buttonArea.setLayout(new GridLayout(4, 1, 0, 5));
		classContent.add(buttonArea, BorderLayout.EAST);
		
		classIDField.setEditable(false);
		
		allClassesBox.addItem("New class");
		
		if (allClasses.size() == 0) {
			allClassesBox.addItem("No classes");
		}
		
		for (int i = 0; i < allClasses.size(); ++i) {
			
			allClassesBox.addItem(allClasses.get(i));
			
		}
		
		allClassesBox.addItemListener(new ItemListener() {
			@Override
			public void itemStateChanged(ItemEvent e) {
				
				if (e.getItem() == "New class") {

					addBtn.setEnabled(true);
					editBtn.setEnabled(false);
					removeBtn.setEnabled(false);
					
					classIDField.setText("");
					classNameField.setText("");
					classStaffField.setText("");
					
				}
				else if (e.getItem() != "New class") {

					addBtn.setEnabled(false);
					editBtn.setEnabled(true);
					removeBtn.setEnabled(true);
					
					classIDField.setText("");
					classNameField.setText("");
					classStaffField.setText("");
					
					try {
						
						Class cl = (Class) e.getItem();
						
						classIDField.setText("" + cl.getID());
						classNameField.setText(cl.getName());
						classStaffField.setText(cl.getStaff().getName());
						
					}
					catch (Exception ex) {
						System.out.println("Exception: " + ex);
					}
					
				}
				
			}
		});
		
		buttonArea.add(allClassesBox);
		
		addBtn.setPreferredSize(new Dimension(150, 20));
		addBtn.setMinimumSize(new Dimension(150, 20));
		addBtn.setMaximumSize(new Dimension(150, 20));
		
		addBtn.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent event) {
				
				Class cl = new Class();
				
				if (!classNameField.getText().isEmpty()) {
					
					cl.setName(classNameField.getText());
					
					if (!classStaffField.getText().isEmpty()) {
						
						if (integerChecker(classStaffField.getText())) {
							
							cl .setStaff(sc.findStaffID(Integer.parseInt(classStaffField.getText())));
							
						}
						else {
							
							cl.setStaff(sc.findSingleStaffName(classStaffField.getText()));
							
						}
						
					}
					
					cc.addClass(cl);
					
					if (allClassesBox.getItemAt(1) == "No classes") {
						allClassesBox.removeItemAt(1);
					}
					
					cl.setID(cc.highestID());
					
					allClassesBox.addItem(cl);
					allClassesBox.setSelectedIndex((allClassesBox.getItemCount() - 1));
					
				}
				else {
					new EmptyField("Class name");
				}
				
			}
		});
		
		buttonArea.add(addBtn);
		
		editBtn.setPreferredSize(new Dimension(150, 20));
		editBtn.setMinimumSize(new Dimension(150, 20));
		editBtn.setMaximumSize(new Dimension(150, 20));
		editBtn.setEnabled(false);
		
		editBtn.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent event) {
				
				Class cl = new Class();
				Staff staff = null;
				
				cl.setStaff(staff);
				
				if (!classIDField.getText().isEmpty()) {
					
					cl.setID(Integer.parseInt(classIDField.getText()));
					cl.setName(classNameField.getText());
					
					if (!classStaffField.getText().isEmpty()){
						
						if (!integerChecker(classStaffField.getText())){
						
							cl.setStaff(sc.findSingleStaffName(classStaffField.getText()));
						
						}
						else {
						
							cl.setStaff(sc.findStaffID(Integer.parseInt(classStaffField.getText())));
						
						}
						
					}
					
					cc.updateClass(cl);
					
					allClassesBox.removeItemAt(allClassesBox.getSelectedIndex());
					allClassesBox.addItem(cl);
					allClassesBox.setSelectedItem((allClassesBox.getItemCount() - 1));
					
				}
				
			}
		});
		
		buttonArea.add(editBtn);
		
		removeBtn.setPreferredSize(new Dimension(150, 20));
		removeBtn.setMinimumSize(new Dimension(150, 20));
		removeBtn.setMaximumSize(new Dimension(150, 20));
		removeBtn.setEnabled(false);
		
		removeBtn.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent event) {
				
				String deletion = "Class: " + classIDField.getText() + ", " + classNameField.getText();
				
				DeleteWarning dw = new DeleteWarning(deletion);
				
				if (dw.getAnswer()) {
					cc.removeClass(Integer.parseInt(classIDField.getText()));
				
					allClassesBox.removeItemAt(allClassesBox.getSelectedIndex());
				}
				
			}
		});
		
		buttonArea.add(removeBtn);
		
		textNameArea.add(new JLabel("ID"));
		textNameArea.add(new JLabel("Name"));
		textNameArea.add(new JLabel("Staff"));
		
		textArea.add(classIDField);
		textArea.add(classNameField);
		textArea.add(classStaffField);
		
	}
	
	public boolean integerChecker (String testString) {
		
		try{
			
			Integer.parseInt(testString);
	         return true;
			
		}
		catch (NumberFormatException e) {
			
			return false;
			
		}
	}

}
